#!/bin/sh
# misc functions

# $1 = device_name (ex: sda1, mmcblk0p1, sr0 - returns sda, mmcblk0, sr0)
fx_get_drvname() {
	case $1 in
		mmc*|nmve*) echo ${1%p*} ;; #remove trailing p*
		*) echo ${1:0:3} ;;         #assume sda, sr0, xxx
	esac
}

# $1 = bytes
fx_format_bytes() {
	[ $1 ] || return
	zzBYTES=$1
	zzFORMATTEDSIZE="${zzBYTES}B" # bytes
	if [ ${zzBYTES} -ge 1099511627776 ] ; then
		zzFORMATTEDSIZE="`dc $zzBYTES 1099511627776 \/ p`"
		zzFORMATTEDSIZE="`printf "%.2f" $zzFORMATTEDSIZE`T" #terabytes
	elif [ ${zzBYTES} -ge 1073741824 ] ; then
		zzFORMATTEDSIZE="`dc $zzBYTES 1073741824 \/ p`"
		zzFORMATTEDSIZE="`printf "%.1f" $zzFORMATTEDSIZE`G" # gigabytes
	elif [ ${zzBYTES} -ge 1048576 ] ; then
		zzFORMATTEDSIZE="$(( $zzBYTES / 1048576 ))M" # megabytes
	elif [ ${zzBYTES} -ge 1024 ] ; then
		zzFORMATTEDSIZE="$(( $zzBYTES / 1024 ))K" # kilobytes
	fi
	echo $zzFORMATTEDSIZE
}
